﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Irandeb4.Models;

namespace Irandeb4.Controllers
{
    [Authorize(Roles = "Administrator , Employee")]
    public class SupplierManagerController : Controller
    {
        private IrandebEntities db = new IrandebEntities();



        public ViewResult Details(int id)
        {
            Company company = db.companies.Find(id);
            ViewBag.LegendLabel = "Empleado Contacto";
            return View(company);
        }

        //
        // GET: /SupplierManager/Create

        public ActionResult Create()
        {
            ViewBag.CityId = new SelectList(db.cities, "CityId", "Name");
            ViewBag.LegendLabel = "Empleado Contacto";
            return View();
        } 

        //
        // POST: /SupplierManager/Create

        [HttpPost]
        public ActionResult Create(Company company)
        {
            if (!NitExists(company))
            {
                    if (ModelState.IsValid)
                    {
                        company.Active = true;
                        company.Coustomer = false;
                        db.companies.Add(company);
                        db.SaveChanges();
                        return RedirectToAction("Search");
                    }
                    else ViewBag.MessageError = "";
            }
            else
                ViewBag.MessageError = "El NIt ya existe";

            ViewBag.CityId = new SelectList(db.cities, "CityId", "Name", company.CityId);
            ViewBag.LegendLabel = "Empleado Contacto";
            return View(company);
        }
        
 
        public ActionResult Edit(int id)
        {
            Company company = db.companies.Find(id);
            ViewBag.CityId = new SelectList(db.cities, "CityId", "Name", company.CityId);
            ViewBag.LegendLabel = "Empleado Contacto";
            return View(company);
        }

        //
        // POST: /SupplierManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Company company)
        {
            if (!NitExistsOther(company))
            {
                if (!CiExistsOther(company.Employee))
                {
                    if (ModelState.IsValid)
                    {
                        //db.Entry(company).State = EntityState.Modified;
                        Company companyToSave = db.companies.Find(company.CompanyId);
                        companyToSave.Nit = company.Nit;
                        companyToSave.Name = company.Name;
                        companyToSave.Phone = company.Phone;
                        companyToSave.Fax = company.Fax;
                        companyToSave.Email = company.Email;
                        companyToSave.Address = company.Address;
                        companyToSave.CityId = company.CityId;
                        //companyToSave.Active = company.Active;
                        //companyToSave.Coustomer = company.Coustomer;

                        companyToSave.Employee.Ci = company.Employee.Ci;
                        companyToSave.Employee.Names = company.Employee.Names;
                        companyToSave.Employee.Surnames = company.Employee.Surnames;
                        companyToSave.Employee.Email = company.Employee.Email;
                        companyToSave.Employee.Cellphone = company.Employee.Cellphone;
                        companyToSave.Employee.Address = company.Employee.Address;
                        companyToSave.Employee.Position = company.Employee.Position;

                        db.SaveChanges();
                        return RedirectToAction("Search");
                    }
                    else
                        ViewBag.MessageError = "";
                }
                else
                    ViewBag.MessageError = "El CI ya existe";
            }
            else
                ViewBag.MessageError = "El NIt ya exste";

            ViewBag.CityId = new SelectList(db.cities, "CityId", "Name", company.CityId);
            ViewBag.LegendLabel = "Empleado Contacto";
            return View(company);
        }

 
        public ActionResult Delete(int id)
        {
            Company company = db.companies.Find(id);
            ViewBag.LegendLabel = "Empleado Contacto";
            return View(company);
        }


        [HttpPost]
        public ActionResult DeleteConfirmed(int id, Company company)
        {
            Company companyToChange = db.companies.Find(id);
            companyToChange.Active = company.Active;
            db.SaveChanges();
            return RedirectToAction("Search");
        }

        public ActionResult Search()
        {
            var companies = db.companies;
            return View(companies.ToList());
        }


        [HttpPost]
        public ActionResult Search(String toSearch, String searchCriteria)
        {
            List<Company> results = new List<Company>();
            foreach (var company in db.companies)
            {
                if (company.Name.Contains(toSearch) || company.Email.Contains(toSearch))
                    if (!company.Coustomer)
                    {
                        if (searchCriteria == "active" && company.Active)
                            results.Add(company);
                        if (searchCriteria == "inactive" && !company.Active)
                            results.Add(company);
                        if (searchCriteria == "all")
                            results.Add(company);
                    }
            }

            foreach (var employee in db.Employees)
            {
                if (employee.Names.Contains(toSearch) || employee.Surnames.Contains(toSearch) || employee.Email.Contains(toSearch))
                {
                    //Company company = db.companies.Find(employee.EmployeeId);
                    IEnumerable<Company> companies = from c in db.companies
                                    where c.EmployeeId == employee.EmployeeId
                                    select c;
                    
                    Company company = companies.ElementAtOrDefault(0);
                    bool exists = false;
                    
                    foreach (var companiesSelected in results)
                    {
                        if (company!=null && companiesSelected.CompanyId == company.CompanyId)
                            exists = true;


                        if (company!=null &&!exists && !company.Coustomer)
                        {
                            if (searchCriteria == "active" && company.Active)
                                results.Add(company);
                            if (searchCriteria == "inactive" && !company.Active)
                                results.Add(company);
                            if (searchCriteria == "all")
                                results.Add(company);
                        }
                    }

                }
            }

            return View(results);
        }

        public bool NitExists(Company company)
        {
            var companies = from c in db.companies
                            where c.Nit == company.Nit
                            select c;

            //Company company1 = comp;
            if (companies.Count() > 0)
                return true;
            else
                return false;
        }

        public bool NitExistsOther(Company company)
        {
            var companies = from c in db.companies
                            where c.Nit == company.Nit
                            select c;

            //Company company1 = comp;
            if (companies.Count() > 1)
                return true;
            else
                return false;
        }

        public bool CiExistsOther(Employee employee)
        {
            var employees = from c in db.Employees
                            where c.Ci == employee.Ci
                            select c;

            if (employees.Count() > 1)
                return true;
            else
                return false;
        }

        public bool CiExists(Employee employee)
        {
            var employees = from c in db.Employees
                            where c.Ci == employee.Ci
                            select c;

            if (employees.Count() > 0)
                return true;
            else
                return false;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}